<?php
/**
 * 默认控制器
 * @author Administrator
 *
 */
class Main extends MY_Controller{
    function main(){
        parent::MY_Controller();
    }
    /**
     * 根据角色跳转
     */
    function index(){
        if ($this->user['userRole'] == 1){
            redirect(printUrl('Main','storeTotal'));
        }else{
            redirect(printUrl('Main','adminTotal'));
        }
    }
    /**
     * 专营店总控表
     */
    function storeTotal(){
        if ($this->user['userRole'] != 1){
            showError('您无权限进去此页面');
        }
        $this->viewData['title']    = '百县强基汇总表';
        //载入模型
        $this->load->model('singleProcessModel');
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位配置
        $this->load->model('timeUnitModel');
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getTimeUnitList($year);
        $where = array('storeId'=>$this->user['userId'], 'year'=>$year);
        $this->viewData['singleProcList'] = $this->singleProcessModel->getNewListByTimeUnit($where);
        
        $this->load->view('Main/storeTotal', $this->viewData);
    }
    /**
     * 管理端汇总表
     */
    function adminTotal(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进入此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Main', 'adminTotal'),'title'=>'费用总控表');
        //条件
        $viewStoreIds = $this->userLib->getViewStoreIds();
        $where = array();
        if ($viewStoreIds == '*'){
            $where['storeId !='] = 0;
        }else{
            $where['storeId'] = array('in', $viewStoreIds);
        }
        //年
        $this->load->model('timeUnitModel');
        $this->viewData['year'] = $this->input->get('year');
        $this->viewData['year'] || $this->viewData['year'] = date('Y');
        $where['year'] = $this->viewData['year'];
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] || $this->viewData['timeUnit'] = $this->timeUnitModel->getNowTimeUnit();
        $where['timeUnit'] = $this->viewData['timeUnit'];
        //基金类型
        $this->viewData['fundType'] = (int)$this->input->get('fundType');
        $this->viewData['fundType'] || $this->viewData['fundType'] = 1;
        //显示类型
        $showType = (int)$this->input->get('showType');
        $showType || $showType = 1;
        $this->viewData['showType'] = $showType;
        //查找符合条件的主线流程数据
        $this->load->model('singleProcessModel');
        $procList = $this->singleProcessModel->getNewListByStoreId($where);
        $this->viewData['procList'] = $procList;
        //树结构地区列表
        $this->viewData['placeTree'] = $this->userLib->getPlaceTree();
        //获取地区相关缓存数据
        $this->load->model('sellpointModel');
        //小区对应专营店列表
        $spsareaToStoreList = $this->sellpointModel->getCacheData('spsareaToStoreList');
        $this->viewData['spsareaToStoreList'] = $this->sellpointModel->getCacheData('spsareaToStoreList');
        //大区对应专营店列表
        $this->viewData['regionToStoreList'] = $this->sellpointModel->getCacheData('regionToStoreList');
        //地区对应专营店列表
        $this->viewData['areaToStoreList'] = $this->sellpointModel->getCacheData('areaToStoreList');
        $subType = $this->input->get('subType');
        $subType || $subType = 'query';
        $this->viewData['subType'] = $subType;
        if ($subType == 'query'){
            $this->load->view('Main/adminTotal', $this->viewData);
        }elseif($subType == 'export'){
            if ($showType == 1){
                $fileName = '全部';
            }elseif ($showType == 2){
                $fileName = '大区';
            }
            $fileName .= $this->viewData['yearList'][$this->viewData['year']];
            $fileName .= '-'.$this->viewData['timeUnitList'][$this->viewData['timeUnit']];
            //设置文件下载头
            setExcelDownHeader($fileName);
            $content = '';
            if ($showType == 1){
                $content = $this->load->view('Main/part/singleSpsareaTable', $this->viewData, true);
            }elseif($showType == 2){//大区形式
                $content = $this->load->view('Main/part/singleRegionTable', $this->viewData, true);
            }
            echo @ereg_replace("<a [^>]*>|<\/a>", "", $content);
        }
    }
    function adminDetail(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进入此页面');
        }
        //查询条件
        $where = array();
        //年
        $this->load->model('timeUnitModel');
        $this->viewData['year'] = $this->input->get('year');
        $this->viewData['year'] || $this->viewData['year'] = date('Y');
        $where['year'] = $this->viewData['year'];
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] || $this->viewData['timeUnit'] = $this->timeUnitModel->getNowTimeUnit();
        $where['timeUnit'] = $this->viewData['timeUnit'];
        //系统配置
        $this->viewData['yearList'] = $this->timeUnitModel->getCacheData('yearList');
        $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
        $this->viewData['timeUnitList'] = $timeUnitList[$this->viewData['year']];
        //基金类型
        $this->viewData['fundType'] = (int)$this->input->get('fundType');
        $this->viewData['fundType'] || $this->viewData['fundType'] = 1;
        //载入模型
        $this->load->model('sellpointModel');
        //地区
        $area = $this->input->get('area');
        $area = urldecode($area);
        $this->viewData['area'] = $area;
        //大区
        $region = $this->input->get('region');
        $region = urldecode($region);
        $this->viewData['region'] = $region;
        //小区
        $spsarea = $this->input->get('spsarea');
        $spsarea = urldecode($spsarea);
        $this->viewData['spsarea'] = $spsarea;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea);
        $placeStoreIds = array_keys($placeStoreList);
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        //查找符合条件的主线流程数据
        $this->load->model('singleProcessModel');
        $procList = $this->singleProcessModel->getNewList($where);
        $this->viewData['procList'] = $procList;
        //所有专营店数据
        $this->viewData['allStoreList'] = $placeStoreList;
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Main', 'adminTotal',array('year'=>$this->viewData['year'],'timeUnit'=>$this->viewData['timeUnit'],'fundType'=>$this->viewData['fundType'])),'title'=>'费用总控表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Main', 'adminList',array('year'=>$this->viewData['year'],'timeUnit'=>$this->viewData['timeUnit'],'fundType'=>$this->viewData['fundType'])),'title'=>'费用明细');
        //提交类型
        $subType = $this->input->get('subType');
        $subType || $subType = 'query';
        $this->viewData['subType'] = $subType;
        if ($subType == 'query'){
            $this->load->view('Main/adminDetail', $this->viewData);
        }elseif($subType == 'export'){
            $fileName = '';
            $area && $fileName .= $area.'区-';
            $region && $fileName .= $region.'-';
            $spsarea && $fileName .= $spsarea.'-';
            $fileName .= $this->viewData['yearList'][$this->viewData['year']];
            $fileName .= '-'.$this->viewData['timeUnitList'][$this->viewData['timeUnit']];
            //设置文件下载头
            setExcelDownHeader($fileName);
            $content = $this->load->view('Main/part/singleDetailTable', $this->viewData, true);
            echo @ereg_replace("<a [^>]*>|<\/a>", "", $content);
        }
    }
    /**
     * 提交评分系数
     */
    function adminSubScore(){
        $_POST || showError('非法请求');
        (isset($_POST['subData']) && $_POST['subData']) || showError('请设置提交数据');
        $this->load->model('singleProcessModel');
        foreach ((array)$_POST['subData'] as $k => $v){
            $procData = $this->singleProcessModel->getData((int)$k);
            if (!$this->auth->isSingleSettleSubScore($procData)){
                showError('您没有评分权限');
            }
            $this->singleProcessModel->edit((int)$k, array('score'=>(float)$v['score']));
            $this->singleProcessModel->updateMoney((int)$k);
        }
        showSuccess('提交评分系数成功');
    }
    function test(){
        $this->load->view('Main/index');
    }
}